package com.my.screenadapter.Base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.util.Log;
import android.widget.Toast;

import com.my.screenadapter.PermissionListererInterface;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 张英杰 on 2016/11/15.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private static final int requestPermissionCode = 1010;
    protected Context mContext;
    private static PermissionListererInterface mPermissionListerer;
    public final String TAG = this.getClass().getName();
    //执行顺序
    //onCreate====: BaseTActivity--
    //onCreate===: MyPermission--
    //====: onDestroy BaseTActivity1--
    //   base super.onDestroy();
    //====: onDestroy BaseTActivity2--
    //====: MyPermission--onDestroy
    private Unbinder mBinder;

    //申请权限
    public static void requestRuntimePermission(String[] permission, PermissionListererInterface permissionListerer) {
        Activity topActivity = ActivityCollector.getTopActivity();
        if (topActivity == null) {
            return;
        }
        mPermissionListerer = permissionListerer;
        //一次申请多个权限
        List<String> permissionList = new ArrayList<>();
        for (String s : permission) {
            if (ContextCompat.checkSelfPermission(topActivity, s)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(s);
            }
        }
        if (!permissionList.isEmpty()) {
            ActivityCompat.requestPermissions(topActivity,
                    permissionList.toArray(new String[permissionList.size()]),
                    requestPermissionCode);
        } else {
            Toast.makeText(topActivity, "所有权限都同意了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread thread = Thread.currentThread();
        Log.i(TAG, "onCreate: activitytaskid "+getTaskId()+" thread name:"+thread.getName()+" threadid:"+thread.getId()+"  pid: "+android.os.Process.myPid());
        mContext = this;
        ActivityCollector.addActivity(this);
        setContentView(initLayoutId());
        mBinder = ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, "onNewIntent: ");
    }

    /**
     *  从onStop回到Activity的时候会执行
     按HOME键的时候会执行onStop,重新回到程序会执行这个方法
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart: ");
    }

    /**
     * 在onCreate,onRestart后面执行
     */
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    /**
     * 在onStart后面执行，执行这个方法后这个Activity就处于全部Activity堆栈的最上面
     * 进入用户可见可操作的状态
     */
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    /**
     * 当其他Activity启动时这个方法会执行
     * 按HOME和BACK都会执行这个方法
     * 最好在这个方法中提交或者保存数据，因为很有可能再也不会回到这个activity中。
     * 这个方法最好不要执行太长时间，因为下个activity开始执行前会等待这个方法返回。
     */
    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }
    

    protected abstract void init();

    @LayoutRes
    protected abstract int initLayoutId();

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case requestPermissionCode:
                if (grantResults.length > 0) {
                    List<String> deniedPermission = new ArrayList<>();
                    List<String> grantedPermission = new ArrayList<>();
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                            deniedPermission.add(permissions[i]);
                        } else {
                            grantedPermission.add(permissions[i]);
                        }
                    }
                    if (!deniedPermission.isEmpty()) {
                        mPermissionListerer.onDenied(deniedPermission);
                    }
                    if (!grantedPermission.isEmpty()) {
                        mPermissionListerer.onGranted(grantedPermission);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
        ActivityCollector.removeActivity(this);
        mBinder.unbind();
        //2017-12-16 22:21:45
    }
}
